<template>
  <div>
    <el-form label-width="80px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="name">
        <el-input placeholder="请输入账号" v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          placeholder="请输入密码"
          v-model="account.password"
          show-password
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { ElFormItem, ElForm } from 'element-plus'
import { rules } from '../config/account-config'
import localcache from '../../../utils/cache'
export default defineComponent({
  components: {
    ElFormItem
  },
  setup() {
    const store = useStore()
    const account = reactive({
      name: localcache.getcache('name') ?? '',
      password: localcache.getcache('password') ?? ''
    })
    const formRef = ref<InstanceType<typeof ElForm>>()
    const loginActive = (isKeepPassword: boolean) => {
      formRef.value?.validate((valid) => {
        if (valid) {
          if (isKeepPassword) {
            localcache.setcache('name', account.name)
            localcache.setcache('password', account.password)
          } else {
            localcache.deletecache('name')
            localcache.deletecache('password')
          }
        }
        store.dispatch('login/accountLoginActive', { ...account })
      })
    }
    return {
      store,
      rules,
      account,
      formRef,
      loginActive,
      localcache
    }
  }
})
</script>

<style scoped lang="less"></style>
